{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "follow on insta\n",
    "<a href=\"https://www.instagram.com/programming_fever/\">@programmimg_fever</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LicensePlateRecognition using OpenCV python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "programming_fever's License Plate Recognition\n",
      "\n",
      "Detected license plate Number is: MH 20 EE 7598\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import imutils\n",
    "import numpy as np\n",
    "import pytesseract\n",
    "pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'\n",
    "\n",
    "img = cv2.imread('D://skoda1.jpg',cv2.IMREAD_COLOR)\n",
    "img = cv2.resize(img, (600,400) )\n",
    "\n",
    "gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) \n",
    "gray = cv2.bilateralFilter(gray, 13, 15, 15) \n",
    "\n",
    "edged = cv2.Canny(gray, 30, 200) \n",
    "contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
    "contours = imutils.grab_contours(contours)\n",
    "contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]\n",
    "screenCnt = None\n",
    "\n",
    "for c in contours:\n",
    "    \n",
    "    peri = cv2.arcLength(c, True)\n",
    "    approx = cv2.approxPolyDP(c, 0.018 * peri, True)\n",
    " \n",
    "    if len(approx) == 4:\n",
    "        screenCnt = approx\n",
    "        break\n",
    "\n",
    "if screenCnt is None:\n",
    "    detected = 0\n",
    "    print (\"No contour detected\")\n",
    "else:\n",
    "     detected = 1\n",
    "\n",
    "if detected == 1:\n",
    "    cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)\n",
    "\n",
    "mask = np.zeros(gray.shape,np.uint8)\n",
    "new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)\n",
    "new_image = cv2.bitwise_and(img,img,mask=mask)\n",
    "\n",
    "(x, y) = np.where(mask == 255)\n",
    "(topx, topy) = (np.min(x), np.min(y))\n",
    "(bottomx, bottomy) = (np.max(x), np.max(y))\n",
    "Cropped = gray[topx:bottomx+1, topy:bottomy+1]\n",
    "\n",
    "text = pytesseract.image_to_string(Cropped, config='--psm 11')\n",
    "print(\"programming_fever's License Plate Recognition\\n\")\n",
    "print(\"Detected license plate Number is:\",text)\n",
    "img = cv2.resize(img,(500,300))\n",
    "Cropped = cv2.resize(Cropped,(400,200))\n",
    "cv2.imshow('car',img)\n",
    "cv2.imshow('Cropped',Cropped)\n",
    "\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "you can find code description @medium publication\n",
    "<a href=\"https://medium.com/programming-fever\">@programmimg_fever</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
